﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace FinGu.Testing.Config
{
    public class Config : AbsConfig
    {
        public override void WriteConfig(string key, string value)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }

            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 
        }
        public override bool ReadConfig(string sSection, string sName, 
            ref string sKey, ref string sValue, ref string info)
        {
            try
            {
                // Get the configuration file.
                Configuration config =
                  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                // Get the AppSetins section.
                AppSettingsSection appSettingSection =
                    (AppSettingsSection)config.GetSection(sSection);// ("appSettings");

                // return key or value.
                //string str2 = config.AppSettings.Settings["SqlConnectionString"].Value;
                sKey = config.AppSettings.Settings[sName].Key;

                sValue = config.AppSettings.Settings[sName].Value;

                info = "配置信息读取成功！";

                return true;
            }
            catch (Exception exc)
            {
                info = exc.Message;

                return false;
            }
        }
    }
}
